<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User as userModel;

class Login extends Controller

{
    public function login()
    {
       return view('login');
    }
    public function login_ok(Request $request)
    {
        $uname = $request->input('uname');
        if(!$uname==''){
            $upassword = md5($request->input('upassword'));
            $user = new userModel();
            $rows = $user::where('uname',$uname)->first();
            if(!$rows){
                return ['code'=>-1,'name'=>'用户不存在'];
            }else if($rows['upassword']!=$upassword){
                return ['code'=>-2,'name'=>'密码错误'];
            }else{
                $request->session()->put('uname',$uname);
                return ['code'=>1,'name'=>'登录成功','url'=>'/'];
            }
        }else{
            return ['code'=>-3,'name'=>'用户名不能为空'];
        }
    }
    public function login_out(Request $request)
    {
        $session = $request->session()->forget('uname');
        if($session==null){
            return redirect('/member');
        }
    }
}
